﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using CUREEntity;
using CUREDataAccessLayers.Common;
using DBUtility;

namespace CUREDataAccessLayers
{
    public class DeptDal
    {
        private common comm = new common();

        /// <summary>
        /// 获取部门详细信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <param name="pageIndex">当前页的索引</param>
        /// <param name="pageSize">每页显示的条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns></returns>
        public DataSet GetDeptData(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            //IList<DeptEntity> list = null;
            StringBuilder sql = new StringBuilder();
            DataSet ds = new DataSet();
            string dept_id = string.Empty;
            string dept_name = string.Empty;
            #region 处理数据
            if (where.ContainsKey("dept_id") && where["dept_id"] != null && where["dept_id"].ToString() != "")
            {
                dept_id = where["dept_id"].ToString();
            }
            if (where.ContainsKey("dept_name") && where["dept_name"] != null && where["dept_name"].ToString() != "")
            {
                dept_name = where["dept_name"].ToString();
            }
            #endregion
            //sql.Append("select t.id,t.dept_id,t.dept_name from department t where 1=1 ");
            if (dept_id != null && dept_id != "")
            {
                sql.Append(" and t.dept_id='" + dept_id.Trim() + "'");
            }
            if (dept_name != null && dept_name != "")
            {
                sql.Append(" and t.dept_name='" + dept_name.Trim() + "'");
            }
            try
            {
                ds = comm.GetList("department", pageSize, pageIndex, sql.ToString(), "", out  totalRecordCount, out totalPageCount);
                
                //list = this.DbHelperInstance.GetPagedData<DeptEntity>(pageIndex, pageSize, sql, " to_number(id) asc ", out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        /// <summary>
        /// 根据id获取部门名称
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>部门名称</returns>
        public string GeDeptNameById(string dept_id)
        {
            string name = string.Empty;
            StringBuilder sql = new StringBuilder();
            sql.Append(" select t.dept_name from department t where t.dept_id='" + dept_id + "' ");
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    name = obj.ToString();
                }
                else
                {
                    name = "";
                }
                //name = this.DbHelperInstance.ExecuteScalar<string>(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return name;
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="where">参数</param>
        /// <returns>true 新增成功/fasle 新增失败</returns>
        public bool InsertDeptData(IDictionary<string, object> where)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            string id = GetMaxId().ToString();
            string dept_id = string.Empty;
            string dept_name = string.Empty;
            #region 处理数据
            if (where.ContainsKey("dept_id") && where["dept_id"] != null && where["dept_id"].ToString() != "")
            {
                dept_id = where["dept_id"].ToString().Trim();
            }
            if (where.ContainsKey("dept_name") && where["dept_name"] != null && where["dept_name"].ToString() != "")
            {
                dept_name = where["dept_name"].ToString().Trim();
            }
            #endregion
            bool judge = JudgeDeptIDExist(dept_id);
            if (judge == false)
            {
                sql.Append("insert into department t values('" + id + "','" + dept_id + "','" + dept_name + "')");
                try
                {
                    int i = DbHelperOra.ExecuteSql(sql.ToString());
                   // int i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                    if (i == 1)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return result;
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="where">参数</param>
        /// <returns>true 修改成功/fasle 修改失败</returns>
        public bool UpdateDeptData(IDictionary<string, object> where)
        {
            bool result = false;
            string dept_id = string.Empty;
            string dept_name = string.Empty;
            string dept_idold = string.Empty;
            StringBuilder sql = new StringBuilder();
            #region 处理数据
            if (where.ContainsKey("dept_id") && where["dept_id"] != null && where["dept_id"].ToString() != "")
            {
                dept_id = where["dept_id"].ToString().Trim();
            }
            if (where.ContainsKey("dept_name") && where["dept_name"] != null && where["dept_name"].ToString() != "")
            {
                dept_name = where["dept_name"].ToString().Trim();
            }
            #endregion
            sql.Append("update department t set t.dept_name='" + dept_name + "' where t.dept_id='" + dept_id + "' ");
            try
            {
                int i = DbHelperOra.ExecuteSql(sql.ToString());
                //int i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                if (i == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>true 删除成功/fasle 删除失败</returns>
        public bool DeleteDeptData(string deptid)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            sql.Append("begin ");
            sql.Append(" delete from department t where t.dept_id='" + deptid + "'; ");
            sql.Append(" update master t set t.dept = '' where t.dept = '" + deptid + "'; ");
            sql.Append(" commit;");
            sql.Append(" end;");
            try
            {
                DbHelperOra.ExecuteSql(sql.ToString());
                //this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns>返回最大的id+1</returns>
        public int GetMaxId()
        {
            int maxid = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(to_number(id))+1 from department t");

                maxid = DbHelperOra.GetMaxID("id", "department");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxid;
        }

        /// <summary>
        /// 判断部门编号是否存在
        /// </summary>
        /// <param name="dept_id">部门编号</param>
        /// <returns>true 存在/fasle 不存在</returns>
        public bool JudgeDeptIDExist(string dept_id)
        {
            bool resut = false;
            string deptid = string.Empty;
            StringBuilder sql = new StringBuilder();
            sql.Append("select t.dept_id from department t where t.dept_id='" + dept_id + "'");
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    deptid = obj.ToString();
                }
                else
                {
                    deptid = "";
                }
                //deptid = this.DbHelperInstance.ExecuteScalar<string>(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (deptid != null && deptid != "")
            {
                resut = true;
            }
            return resut;
        }
    }
}
